package com.zues.thread.lock.correct_posture;

import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "c.Step1")
public class Step1 {

    static final Object room = new Object();
    //有没有烟
    static boolean hasCigarette = false;
    static boolean hasTakeout = false;

    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            synchronized (room) {
                log.debug("有烟没？{}", hasCigarette);
                if(!hasCigarette){
                    try {
                        room.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                log.debug("有烟没？{}", hasCigarette);
                if(hasCigarette){
                    log.debug("可以开始干活了");
                }
            }
        }, "小南").start();

        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                synchronized (room){
                    log.debug("可以开始干活了");
                }
            }, "其它人" + i).start();
        }

        Thread.currentThread().sleep(1);

        new Thread(() -> {
            synchronized (room){
                hasCigarette = true;
                log.debug("烟到了噢！");
                room.notify();
            }
        }, "送烟的").start();

    }

}
